package com.app.controller.miniprogram;

import com.app.common.Result;
import com.app.mapper.ActivityEnlistMapper;
import com.app.model.ActivityEnlistModel;
import com.app.model.ActivityModel;
import com.app.model.UserModel;
import com.app.service.ActivityService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.concurrent.AbstractExecutorService;

@Controller
@RestController
@RequestMapping("/api/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private UserService userService;

    //分类列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(activityService.selectTypeList());
    }

    //列表查询，通过分类、标题关键字筛选
    @GetMapping("/selectActivityList")
    public Result<Object> selectActivityList(Integer type, String title, Integer page, Integer limit, Integer villageId) throws ParseException {
        return Result.success(activityService.selectActivityList(type, title, page, limit, villageId));
    }

    //查看详情
    @GetMapping("/selectContentById")
    public Result<Object> selectContentById(Integer id, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("authori-zation");
        if (token != null) {
            String mobile = TokenUtils.nothingness(token);
            UserModel user = userService.getUserByOpenid(mobile);
            if (user == null) {
                return Result.error(405, "用户不存在");
            }
            ActivityModel model = activityService.selectContentById(id);
            model.setIsApplication(0);
            model.setIsFiles(0);
            QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", user.getId());
            queryWrapper.eq("activity_id", id);
            ActivityEnlistModel model1 = activityEnlistMapper.selectOne(queryWrapper);

            if (model1 != null) {
                model.setIsApplication(1);

                if (model1.getRemark() != null) {
                    model.setRemark(model1.getRemark());
                }
                if (model1.getFiles() != null && !model1.getFiles().equals("")) {
                    model.setIsFiles(1);
                }
            }
            return Result.success(model);
        }
        if (id == null || id == 0) {
            return Result.success(null);
        }
        return Result.success(activityService.selectContentById(id));
    }

    //报名
    @GetMapping("/application")
    public Result<Object> application(Integer activityId, HttpServletRequest request, UserModel model) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (activityId == null || activityId == 0) {
            return Result.success(user);
        }
        if (user.getVillageId() == 0) {
            return Result.error(406, "您还没有实名认证，认证后即可报名");
        }
        if (!user.getVillageId().equals(model.getVillageId()) && user.getVillageId() != 0) {
            return Result.error(406, "请报名您所在村的活动");
        }
        QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        queryWrapper.eq("activity_id", activityId);
        ActivityEnlistModel model1 = activityEnlistMapper.selectOne(queryWrapper);
        if (model1 != null) {
            return Result.error(500, "已报名");
        }
        if (model.getVillageId() != null && model.getVillageId() != 0) {
            user.setVillageId(model.getVillageId());
        }
        if (model.getName() != null) {
            user.setName(model.getName());
        }
        if (model.getPhoneNumber() != null) {
            user.setPhoneNumber(model.getPhoneNumber());
        }
        if (model.getIdCard() != null) {
            user.setIdCard(model.getIdCard());
        }
        activityService.application(user, activityId);
        return Result.success("报名成功");
    }

    //上传现场图片
    @GetMapping("/upload")
    public Result<Object> upload(Integer activityId, String files, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Integer id = user.getId();
        activityService.upload(files, id, activityId);
        return Result.success("上传成功");
    }

    //查看现场图片
    @GetMapping("/selectFiles")
    public Result<Object> selectFiles(Integer activityId, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Integer userId = user.getId();
        return Result.success(activityService.selectFiles(activityId, userId));
    }
}
